package bb.lanxing.lib.devices.ble.device;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.base.SimpleBluetoothGattCallbackCompat;
import bb.lanxing.lib.devices.base.converter.RadarConverter;
import bb.lanxing.lib.devices.ble.BleCons;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import java.util.ArrayList;
import java.util.UUID;

public class RadarDevice extends AbsBleDevice {
    final private static String TAG = "RadarDevice";
    public static final UUID BLE_RADAR_SERVICE = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
    public static final UUID BLE_RADAR_ORDER = UUID.fromString("6e400004-b5a3-f393-e0a9-e50e24dcca9e");
    public static final UUID BLE_RADAR_NOTIFY = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");

    public RadarDevice(Context context, SmartDevice smartDevice) {
        super(smartDevice);
        init(context, smartDevice.getAddress(), new RadarBluetoothGattCallback(context, this));
    }

    @Override
    public void onServicesDiscovered() {
        setCharacteristicNotification(this.mBluetoothGatt.getService(BLE_RADAR_SERVICE).getCharacteristic(BLE_RADAR_NOTIFY), true);
        readBattery();
    }

    public class RadarBluetoothGattCallback extends SimpleBluetoothGattCallbackCompat<RadarDevice> {
        Context context;
        RadarConverter radarConverter;

        public RadarBluetoothGattCallback(final Context context, RadarDevice radarDevice) {
            super(radarDevice);
            RadarConverter radarConverter = new RadarConverter();
            this.radarConverter = radarConverter;
            this.context = context;
            radarConverter.setOnRadarDataListener(new RadarConverter.OnRadarDataListener() {
                @Override
                public void onRadarData(ArrayList arrayList) {
                    Log.d(TAG, "RadarBluetooth" + "size==" + arrayList.size());
                    Intent intent = new Intent();
                    intent.setAction(BleCons.ACTION_RADAR);
                    intent.putExtra(BleCons.EXTRA_RADAR, arrayList);
                    context.sendBroadcast(intent);
                }
            });
        }

        @Override
        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            this.radarConverter.convert(null, bluetoothGattCharacteristic);
        }
    }
}
